home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / migration-assistant / ma-script-utils
Text File  |  2008-10-29  |  6KB  |  208 lines

  1. #!/bin/sh
  2.  
  3. . /usr/share/debconf/confmodule
  4.  
  5. error () {
  6.     logger -t migration-assistant "error: $@"
  7. }
  8.  
  9. log () {
  10.     logger -t migration-assistant "info: $@"
  11. }
  12.  
  13. ostype=""
  14. set_os_type () {
  15. # Rather than test for every distro possible in the shortname, we test
  16. # the bootloader type for 'linux.'  This *should* be fine as we're only
  17. # working with user's home directories.
  18.  
  19.     if [ ${1##*:} = "linux" ]; then
  20.     ostype="linux"
  21.     return 0
  22.     fi
  23.     
  24.     case `LC_ALL=C expr match "$1" '.*:.*:\(.*\):.*'` in
  25.     "Windows" )
  26.     ostype="windowsxp"
  27.         return 0
  28.     ;;
  29.  
  30.         Windows[0-9] )
  31.         ostype="windowsxp"
  32.         return 0
  33.         ;;
  34.  
  35.     "Windows9xMe" )
  36.     ostype="windows9x"
  37.         return 0
  38.     ;;
  39.  
  40.     "MacOSX" )
  41.     ostype="macosx"
  42.         return 0
  43.     ;;
  44.  
  45.     *)
  46.     echo "Unknown ostype from $1" 1>&2
  47.     return 1
  48.     ;;
  49.     esac
  50. }
  51. mountpoint="/mnt/migrationassistant"
  52.  
  53. unmount_os() {
  54.     # If we didn't mount the device ourselves, don't unmount it.
  55.     [ "$mountpoint" = "/mnt/migrationassistant" ] || return 0
  56.     unmount_previously_run=
  57.     device="$1"
  58.  
  59.     if [ -f /etc/mtab ]; then
  60.         MTAB=/etc/mtab
  61.     else
  62.         MTAB=/proc/mounts
  63.     fi
  64.  
  65.     while :; do
  66.         failed=
  67.         
  68.     ISMOUNTED=
  69.     if [ "$device" ]; then
  70.         ISMOUNTED=$(grep "^$device " $MTAB) || ISMOUNTED=
  71.     else
  72.         ISMOUNTED=$(grep " $mountpoint " $MTAB) || ISMOUNTED=
  73.     fi
  74.         if [ -z "$ISMOUNTED" ]; then
  75.             break
  76.         fi
  77.         HOME=$(grep " $mountpoint/home " $MTAB) || HOME=
  78.         if [ "$HOME" ]; then
  79.             umount "$mountpoint/home" || failed="$mountpoint/home"
  80.         fi
  81.         if [ -z "$failed" ]; then
  82.             if [ -z "$device" ]; then
  83.                 umount $mountpoint || failed="$mountpoint"
  84.             else
  85.                 umount $device || failed="$device"
  86.             fi
  87.         fi
  88.  
  89.         if [ -z "$failed" ]; then
  90.             break
  91.         fi
  92.         # lets try waiting briefly once before we completely give up on
  93.         # unmounting the partition and dump the problem on the user.
  94.         if [ -z "$unmount_previously_run" ]; then
  95.             unmount_previously_run=1
  96.             sleep 15
  97.             continue
  98.         fi
  99.  
  100.         db_reset migration-assistant/failed-unmount
  101.         db_subst migration-assistant/failed-unmount MOUNTED "$failed"
  102.         db_input critical migration-assistant/failed-unmount || true
  103.         db_go || exit 10
  104.         db_get migration-assistant/failed-unmount
  105.         [ "$RET" = true ] || exit 10
  106.     done
  107. }
  108.  
  109. mount_os () {
  110.     ostype="$1"
  111.     device="$2"
  112.  
  113.  
  114.     if [ "$1" = "linux" ]; then
  115.         mkdir -p $mountpoint
  116.         unmount_os $device
  117.         mount $device $mountpoint || error "Failed to mount $device"
  118.         if [ -f "$mountpoint/etc/fstab" ]; then
  119.             while read uuid mp rest; do
  120.                 if [ "$mp" != "/home" ]; then
  121.                     continue
  122.                 fi
  123.  
  124.                 if [ "${uuid%=*}" = "UUID" ]; then
  125.                     devname="/dev/disk/by-uuid/${uuid#*=}"
  126.                     uuid=$(readlink -e "$devname") || \
  127.                         error "$devname does not exist."
  128.                 elif [ "${uuid%=*}" = "LABEL" ]; then
  129.                     devname="/dev/disk/by-label/${uuid#*=}"
  130.                     uuid=$(readlink -e "$devname") || \
  131.                         error "$devname does not exist."
  132.                     elif [ ! -e "$uuid" ]; then
  133.                         # This happens when the IDE driver in the old OS
  134.                         # doesn't match the driver in the installer.  The old
  135.                         # /home might be mounted on /dev/hda3 which could now
  136.                         # be /dev/sda3 or something entirely different.  Since
  137.                         # there's no way to determine what the device is
  138.                         # without the old kernel loaded, we fail gracefully so
  139.                         # that we can continue to the next OS.
  140.                         log "$uuid does not exist, continuing."
  141.                         break
  142.                     fi
  143.                     failed=
  144.                     mount $uuid "$mountpoint/home" || failed="1"
  145.                         if [ "$failed" ]; then
  146.                     unmount_os "$uuid"
  147.                     mount $uuid "$mountpoint/home" || \
  148.                         error "failed to mount $uuid"
  149.                 fi
  150.                 break
  151.             done < "$mountpoint/etc/fstab"
  152.         fi
  153.     elif [ "$1" = "windowsxp" ]; then
  154.         # Since we don't have to worry about separate partitions making up the
  155.         # whole system in Windows (yet), we can allow already mounted
  156.         # partitions.  This may fix some corner cases.  At any rate, it's
  157.         # required for Wubi to function properly.
  158.         mnt=$(grep "$device" /proc/mounts | head -n1 | cut -d " " -f 2)
  159.         [ -z "$mnt" ] && [ -f /etc/mtab ] && mnt=$(grep "$device" /etc/mtab | head -n1 | cut -d " " -f 2)
  160.         [ -n "$mnt" ] && mountpoint=$mnt && return 0
  161.  
  162.         mkdir -p $mountpoint
  163.         unmount_os $device
  164.         mount -t ntfs $device $mountpoint -o umask=0022,nls=utf8 3>&- || \
  165.         ( log "Mounting $device to $mountpoint with NTFS failed."
  166.         mount -t vfat $device $mountpoint -o umask=0022,utf8 || \
  167.         log "Mounting $device to $mountpoint with VFAT failed." )
  168.     fi
  169.  
  170. }
  171.  
  172. ROOT=/target
  173. add_user() {
  174.     local username
  175.     local fullname
  176.     local password
  177.  
  178.     username="$1"
  179.     fullname="$2"
  180.     password="$3"
  181.  
  182.     chroot=chroot
  183.  
  184. # Taken from user-setup/user-setup-apply
  185.  
  186.     # Add the user
  187.     if [ -x $ROOT/usr/sbin/adduser ]; then
  188.         $chroot $ROOT adduser --disabled-password --gecos \
  189.         "$fullname" "$username" >/dev/null || true
  190.     else
  191.         $chroot $ROOT useradd -c "$fullname" -m "$username" >/dev/null || true
  192.     fi
  193.  
  194.     # Set the password
  195.     $chroot $ROOT chpasswd <<EOF
  196. $username:$password
  197. EOF
  198.  
  199.     # Add the user to groups
  200.     if [ -n "$username" ]; then
  201.         db_get passwd/user-default-groups
  202.         for group in $RET; do
  203.             log $chroot $ROOT adduser "$USER" $group >/dev/null 2>&1 || true
  204.         done
  205.     fi
  206. }
  207. # vim:ai:et:sts=4:tw=80:sw=4:
  208.